unit login;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, RzButton, StdCtrls, ComCtrls, ExtCtrls, ActnList;

type
  Tloginfrm = class(TForm)
    lab_usrname: TLabel;
    lab_pswd: TLabel;
    lab_logintm: TLabel;
    dtpk_login: TDateTimePicker;
    edit_pswd: TEdit;
    combx_usr: TComboBox;
    btn_altpswd: TRzButton;
    btn_cntcnfg: TRzButton;
    btn_cancel: TRzBitBtn;
    btn_login: TRzBitBtn;
    Panel1: TPanel;
    lab_version: TLabel;
    actlisten: TActionList;
    actclose: TAction;
    procedure btn_cancelClick(Sender: TObject);
    procedure btn_loginClick(Sender: TObject);
    procedure btn_altpswdClick(Sender: TObject);
    procedure btn_cntcnfgClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure edit_pswdKeyPress(Sender: TObject; var Key: Char);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure combx_usrSelect(Sender: TObject);
    procedure actcloseExecute(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

implementation

{$R *.dfm}

uses includefile, dbmodule, AlterPassword, cnctcfg;

var
  userinfo:TstringList;

procedure Tloginfrm.btn_cancelClick(Sender: TObject);
begin
  ModalResult:=mrCancel;
end;

procedure Tloginfrm.btn_loginClick(Sender: TObject);
var
  paswd:string;
begin
  usrIndex:=combx_usr.ItemIndex;
  username:=Trim(combx_usr.Text);
  paswd:=edit_pswd.Text;
  Case dbmodulefrm.usrlogin(username,paswd,userpwr) of
    -1:begin
      edit_pswd.Text:='';
      combx_usr.Text:='';
      Application.messagebox(pchar(GetCaption('Msgbox',101)),'',16);
      combx_usr.SetFocus;
    end;
    0:begin
      edit_pswd.Text:='';
      Application.messagebox(pchar(GetCaption('Msgbox',102)),'',16);
      combx_usr.SetFocus;
    end;
    1:ModalResult:=mrOk;
  end;
end;

procedure Tloginfrm.btn_altpswdClick(Sender: TObject);
var
  usr:string;
begin 
  usr:=combx_usr.Items.Strings[combx_usr.ItemIndex];
  if usr='' then exit;
  with TAlterpssfrm.Create(Application) do begin
    UserName:=usr;
    ShowModal;
  end;
end;

procedure Tloginfrm.btn_cntcnfgClick(Sender: TObject);
begin
  Tdbcntfrm.Create(Application).ShowModal;
end;

procedure Tloginfrm.FormCreate(Sender: TObject);
var
  I:integer;
begin
  Caption:=GetCaption(Name,1000);
  for I:=0 to ComponentCount-1 do begin
    if Components[I] is TLabel then begin
      TLabel(Components[I]).Caption:=
                GetCaption(Name,TLabel(Components[I]).Tag);
    end;
    if Components[I] is TRzButton then begin
      TRzButton(Components[I]).Caption:=
                GetCaption(Name,TRzButton(Components[I]).Tag);
    end;
  end;
  dtpk_login.Format:=DateFrtStr;
  dtpk_login.DateTime:=now;
  lab_version.Caption:=Format('Version:%-15s',[GetAppVersion(3)]);
  userinfo:=TstringList.Create;
  userinfo.Clear;
  with dbmodulefrm.dbquery do begin
    sql.Clear;
    sql.Add('select * from SYS_USER');
    execsql;
    Active:=True;
    First;
    while not eof do begin
      combx_usr.Items.Add(FieldByName('Name').AsString);
      next;
    end;
  end;
  combx_usr.ItemIndex:=usrIndex;
end;

procedure Tloginfrm.edit_pswdKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then begin
    btn_login.SetFocus;
    btn_loginClick(Sender);
  end;
end;

procedure Tloginfrm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  userinfo.Free;
end;

procedure Tloginfrm.combx_usrSelect(Sender: TObject);
begin
  edit_pswd.SetFocus; 
end;

procedure Tloginfrm.actcloseExecute(Sender: TObject);
begin
  close;
end;

end.
